package db.connectionPool;



//import Configuration.*;
//import ORM.ObjectoRelacionGenerador;
import com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException;
import java.sql.*;

public class DbConnection {
        private static DbConnection instance=null;
        private Connection connection;
    	private Statement stmt;
        //protected ObjectoRelacionGenerador ojbRelGenerator = new ObjectoRelacionGenerador();
        
        private DbConnection(){
        }

        protected static DbConnection initConnection(){
            if(instance == null){
            instance = new DbConnection();
            }else{
            return instance;
            }
            return instance;

         }

	protected void openConection(DBConfiguration dbConfig) throws Exception {
		try {
                    Class.forName(dbConfig.getDriver());

                    this.setConnection(DriverManager.getConnection(dbConfig.getURL(),
                            dbConfig.getDbUser(), dbConfig.getDbPass()));
               
                } catch (ClassNotFoundException e) {
			throw new DataBaseException();
		} catch (SQLException e) {
			throw  new DBNotFoundException();
		}
	}

	protected void closeConection() throws Exception {
		try {
			if (getaStmt() != null) {

				getaStmt().close();
			}
			if (getConnection() != null) {

				getConnection().close();
			}
		} catch (Exception e) {
			throw new DataBaseException();
		}
	}

        public ResultSet executeQuery(String query) throws DataBaseException{
            try {
                setaStmt(getConnection().createStatement());
                ResultSet rs = getaStmt().executeQuery(query);
                return rs;
            } catch (SQLException ex) {
                ex.printStackTrace();
                throw new DataBaseException();
            }catch(Exception ex){
                ex.printStackTrace();
                throw new DataBaseException();
            }
        }


        public void executeUpdate(String query) throws Exception {
		try {
			setaStmt(getConnection().createStatement());
			getaStmt().executeUpdate(query);
		}  catch (MySQLIntegrityConstraintViolationException ex) {
                    throw ex;
                }catch (SQLException e) {
                    DbConnectionManager.getInstance().closeDbConnection(this);
                    e.printStackTrace();
			throw new DataBaseException();
		}
	}


    /**
     * @return the connection
     */
    private Connection getConnection() {
        return connection;
    }

    /**
     * @param connection the connection to set
     */
    private void setConnection(Connection connection) {
        this.connection = connection;
    }

    /**
     * @return the aStmt
     */
    private Statement getaStmt() {
        return stmt;
    }



    /**
     * @param aStmt the aStmt to set
     */
    private void setaStmt(Statement aStmt) {
        this.stmt = aStmt;
    }

    /**
     * @return the configLoader
     */
  

}
